000100 IDENTIFICATION DIVISION.
000200 PROGRAM-ID. USECASE1.
       AUTHOR. GROEP 28.
       AUTHOR. MICHEL GRYP.
       AUTHOR. MICHAEL HEUGHEBAERT.
       AUTHOR. MATTHIAS REYNAERT.
       
       ENVIRONMENT DIVISION.
       SELECT PARAMETERS ASSIGN TO "Parameters.txt"
              ORGANIZATION IS LINE SEQUENTIAL
              FILE STATUS IS FILE-STATUS.
       SELECT CSVIN ASSIGN TO WS-ABSOLUTHPATH
              ORGANIZATION IS LINE SEQUENTIAL
              FILE STATUS IS FILE-STATUS.
       SELECT CSVOUT ASSIGN TO OUT-FILENAME
              ORGANIZATION IS LINE SEQUENTIAL
              FILE STATUS IS FILE-STATUS.
       
       DATA DIVISION.
       FILE SECTION.
       FD PARAMETERS.
       01 PARAMETERLINE                 PIC X(800).
       
       FD CSVIN.
       01 CSVLINE                       PIC X(200).
       
       FD CSVOUT.
       01 OUTLINE.
         03 OUT-MAIL                    PIC X(50).
         03 FILLER                      PIC X VALUE ",".
         03 OUT-PASSWORD                PIC X(10).
           
       WORKING-STORAGE SECTION.
       COPY OPENCLOSECOPY.
       COPY LOGHELPERCOPY.
       COPY FSCOPY.
       COPY SQLCACOPY.
       
       01 WS-LOGMESSAGE                 PIC X(100).
       01 WS-ABSOLUTHPATH               PIC X(250).
       01 JDBCSTRING                    PIC X(800).
       01 OUT-FILENAME                  PIC X(50).
       01 P-END                         PIC X.
         88 EOF                         VALUE "E".
       01 C-END                         PIC X.
         88 CSV-EOF                     VALUE "E".
       
       EXEC SQL 
         BEGIN DECLARE SECTION
       END-EXEC.
       
       COPY CSVLAYOUTCOPY.         
       01 TPCODE                        PIC X(10).
       01 RECORDFOUND                   PIC X.
       01 U-PASSWORD                    PIC X(10).
       
       EXEC SQL 
         END DECLARE SECTION
       END-EXEC
       
       PROCEDURE DIVISION.
       PGM.
       INITIALIZE OPENCLOSE
       
       PERFORM STARTUP
       PERFORM CONNECTDATABASE
       PERFORM READPARAMETERFILE
       PERFORM DISCONNECTDATABASE
       PERFORM SHUTDOWN
      
      * 1. Systeem opent parameterfile
      * 2. Systeem opent logfile
       STARTUP.
       OPEN INPUT PARAMETERS
       PERFORM FSLOGGER

       SET OPENCONNECTION TO TRUE 
       MOVE "UseCase 1*" TO LOGPROGRAM
       MOVE "Starting program*" TO LOGMESSAGE
       PERFORM LOGGER
       INITIALIZE OPENCLOSE
       
      * 1. Systeem connect met de database
      * 2. Systeem leest parameterfile
      * 3. Systeem controleert of er een absoluut pad is naar een csv bestand
      * 4. Systeem controleert of code van toepassing is op intern of een traject
      * 4A. Systeem voegt interne gebruikers toe
      * 4B. Systeem voegt gebruikers toe aan database
       READPARAMETERFILE.     
       READ PARAMETERS
         AT END
           MOVE "No trainingprogramcode found*" TO LOGMESSAGE
           PERFORM LOGGER
           GOBACK
       END-READ
       
       PERFORM UNTIL EOF 
         MOVE PARAMETERLINE TO TPCODE
         
         READ PARAMETERS
           AT END
             MOVE "No CSV-path found*" TO LOGMESSAGE
             PERFORM LOGGER
             GOBACK
         END-READ
         
         MOVE PARAMETERLINE TO WS-ABSOLUTHPATH
         INITIALIZE C-END
         
         IF TPCODE = "intern"
           PERFORM ADDINTERNALUSERS
         ELSE
           PERFORM CHECKTRAININGPROGRAMINDATABASE
           
           IF RECORDFOUND = 1
             PERFORM ADDUSERSTOTRAININGPROGRAM
           END-IF
         END-IF
         
         READ PARAMETERS
           AT END
             SET EOF TO TRUE
         END-READ
       END-PERFORM

       SHUTDOWN.
      * Het systeem sluit de parameter file
       CLOSE PARAMETERS
      * Het systeem sluit de logfile
       SET CLOSECONNECTION TO TRUE
       MOVE "Ending program*" TO LOGMESSAGE
       PERFORM LOGGER
       INITIALIZE OPENCLOSE
       
       STOP RUN
       
      * Functies in verband met database
       CONNECTDATABASE.
      * Systeem leest de parameterfile
       READ PARAMETERS
         AT END
           MOVE "Parameter file is empty*" TO LOGMESSAGE
           PERFORM LOGGER
           PERFORM SHUTDOWN
       END-READ
       
      * Systeem opent de databaseconnectie
       MOVE PARAMETERLINE TO JDBCSTRING
       SET OPENCONNECTION TO TRUE 
       CALL "DatabaseConnection" USING OPENCLOSE, JDBCSTRING END-CALL
       INITIALIZE OPENCLOSE
              
       DISCONNECTDATABASE.
      *Systeem sluit de databaseconnectie
       SET CLOSECONNECTION TO TRUE 
       CALL "DATABASECONNECTION" USING OPENCLOSE END-CALL
       INITIALIZE OPENCLOSE
       
       ADDUSERSTOTRAININGPROGRAM.
       CALL "FILENAMEEXTRACTOR" USING WS-ABSOLUTHPATH 
                                RETURNING OUT-FILENAME
       OPEN INPUT CSVIN
       PERFORM FSLOGGER
       OPEN OUTPUT CSVOUT
       
       IF STATUS-KEY-1 = 0
         READ CSVIN
           AT END
             SET CSV-EOF TO TRUE
             MOVE "Empty CSV-File*" TO LOGMESSAGE
             PERFORM LOGGER
         END-READ
         
         STRING "Adding users to trainingprogram " TPCODE "*"
           DELIMITED BY SIZE
           INTO WS-LOGMESSAGE
         END-STRING
         
         MOVE WS-LOGMESSAGE TO LOGMESSAGE
         PERFORM LOGGER
           
         PERFORM UNTIL CSV-EOF
           PERFORM READCSVLINE
           PERFORM CHECKEXISTINGUSER
           
           IF RECORDFOUND <> 1
             CALL "PASSWORDGENERATOR" RETURNING U-PASSWORD
             EXEC SQL
               INSERT INTO USERS
               VALUES (:E-MAIL, :LNAME, :FNAME, :U-PASSWORD,
               '', '', 0, 0)
             END-EXEC
           
             IF SQLCODE <> 0
               STRING "Error while adding: " E-MAIL "*"
                 DELIMITED BY SIZE
                 INTO WS-LOGMESSAGE
               END-STRING
               
               MOVE WS-LOGMESSAGE TO LOGMESSAGE
               PERFORM LOGGER
             
               MOVE SQLERRMC TO LOGMESSAGE
               PERFORM LOGGER
             ELSE
               MOVE E-MAIL TO OUT-MAIL
               MOVE U-PASSWORD TO OUT-PASSWORD
               WRITE OUTLINE
               
               STRING "Added to users: " E-MAIL "*"
                 DELIMITED BY SIZE
                 INTO WS-LOGMESSAGE
               END-STRING
             
               MOVE WS-LOGMESSAGE TO LOGMESSAGE
               PERFORM LOGGER
             END-IF
           ELSE
             STRING "Duplicate user found in database: " E-MAIL "*"
               DELIMITED BY SIZE
               INTO WS-LOGMESSAGE
             END-STRING
         
             MOVE WS-LOGMESSAGE TO LOGMESSAGE
             PERFORM LOGGER
           END-IF
           
           PERFORM CHECKUSERINTRAININGPROGRAM
           
           IF RECORDFOUND <> 1
             EXEC SQL
               INSERT INTO USERSPERTRAININGPROGRAM
               VALUES (:E-MAIL, :TPCODE)
             END-EXEC
             
             IF SQLCODE <> 0
               STRING "Error while adding: " E-MAIL " to " TPCODE "*"
                 DELIMITED BY SIZE
                 INTO WS-LOGMESSAGE
               END-STRING
               
               MOVE WS-LOGMESSAGE TO LOGMESSAGE
               PERFORM LOGGER
             
               MOVE SQLERRMC TO LOGMESSAGE
               PERFORM LOGGER
             ELSE
               STRING "Added to TP: " E-MAIL "*"
                 DELIMITED BY SIZE
                 INTO WS-LOGMESSAGE
               END-STRING
             
               MOVE WS-LOGMESSAGE TO LOGMESSAGE
               PERFORM LOGGER
             END-IF
           ELSE
             STRING "User already in TP: " E-MAIL "*"
               DELIMITED BY SIZE
               INTO WS-LOGMESSAGE
             END-STRING
         
             MOVE WS-LOGMESSAGE TO LOGMESSAGE
             PERFORM LOGGER
           END-IF
             
           READ CSVIN
           AT END
             SET CSV-EOF TO TRUE
             MOVE "End of CSV-file*" TO LOGMESSAGE
             PERFORM LOGGER
           END-READ
         END-PERFORM
       END-IF
       
       CLOSE CSVIN CSVOUT
       
       ADDINTERNALUSERS.
       CALL "FILENAMEEXTRACTOR" USING WS-ABSOLUTHPATH 
                                RETURNING OUT-FILENAME
       OPEN INPUT CSVIN
       PERFORM FSLOGGER
       OPEN OUTPUT CSVOUT
       
       IF STATUS-KEY-1 = 0
         READ CSVIN
           AT END
             SET CSV-EOF TO TRUE
             MOVE "Empty CSV-File*" TO LOGMESSAGE
             PERFORM LOGGER
         END-READ
         
         MOVE "Adding internal users*" TO LOGMESSAGE
         PERFORM LOGGER
           
         PERFORM UNTIL CSV-EOF
           PERFORM READCSVLINE
           PERFORM CHECKEXISTINGUSER
           
           IF RECORDFOUND <> 1
             CALL "PASSWORDGENERATOR" RETURNING U-PASSWORD
             
             EXEC SQL
               INSERT INTO USERS
               VALUES (:E-MAIL, :LNAME, :FNAME, :U-PASSWORD,
               '', '', 0, 1)
             END-EXEC
           
             IF SQLCODE <> 0
               STRING "Error while adding: " E-MAIL "*"
                 DELIMITED BY SIZE
                 INTO WS-LOGMESSAGE
               END-STRING
               
               MOVE WS-LOGMESSAGE TO LOGMESSAGE
               PERFORM LOGGER
             
               MOVE SQLERRMC TO LOGMESSAGE
               PERFORM LOGGER
             ELSE
               MOVE E-MAIL TO OUT-MAIL
               MOVE U-PASSWORD TO OUT-PASSWORD
               WRITE OUTLINE
               
               STRING "Added to users: " E-MAIL "*"
                 DELIMITED BY SIZE
                 INTO WS-LOGMESSAGE
               END-STRING
             
               MOVE WS-LOGMESSAGE TO LOGMESSAGE
               PERFORM LOGGER
             END-IF
           ELSE
             STRING "Duplicate user found in database: " E-MAIL "*"
               DELIMITED BY SIZE
               INTO WS-LOGMESSAGE
             END-STRING
         
             MOVE WS-LOGMESSAGE TO LOGMESSAGE
             PERFORM LOGGER
           END-IF
           
           READ CSVIN
           AT END
             SET CSV-EOF TO TRUE
             MOVE "End of CSV-file*" TO LOGMESSAGE
             PERFORM LOGGER
           END-READ
         END-PERFORM
       END-IF
       
       CLOSE CSVIN CSVOUT
       
       CHECKTRAININGPROGRAMINDATABASE.
       PERFORM CHECKEXISTINGTRAININGPROGRAMCODE
       
       IF RECORDFOUND = 1
         PERFORM CHECKTRAININGPROGRAMDATE
       END-IF
              
       CHECKEXISTINGTRAININGPROGRAMCODE.
       INITIALIZE RECORDFOUND
       
       EXEC SQL 
         SELECT 1 INTO :RECORDFOUND FROM TrainingProgram 
         WHERE tpCode = :TPCODE
       END-EXEC
       
       IF RECORDFOUND <> 1
         STRING "No trainingprogram found for code: " TPCODE "*"
           DELIMITED BY SIZE
           INTO WS-LOGMESSAGE
         END-STRING
         
         MOVE WS-LOGMESSAGE TO LOGMESSAGE
         PERFORM LOGGER
       END-IF
       
       CHECKTRAININGPROGRAMDATE.
       INITIALIZE RECORDFOUND
       
       EXEC SQL 
         SELECT 1 INTO :RECORDFOUND FROM TrainingProgram 
         WHERE tpCode = :TPCODE
         AND DATEDIFF(DAY, GETDATE(), tpEndOnWeb) > 0
       END-EXEC
       
       IF RECORDFOUND <> 1
         STRING "Trainingprogram enddate expired for code: " TPCODE "*"
           DELIMITED BY SIZE
           INTO WS-LOGMESSAGE
         END-STRING
         
         MOVE WS-LOGMESSAGE TO LOGMESSAGE
         PERFORM LOGGER
       END-IF
              
       CHECKEXISTINGUSER.
       INITIALIZE RECORDFOUND
       
       EXEC SQL 
         SELECT 1 INTO :RECORDFOUND FROM Users 
         WHERE email = :E-MAIL
       END-EXEC
       
       CHECKUSERINTRAININGPROGRAM.
       INITIALIZE RECORDFOUND
       
       EXEC SQL 
         SELECT 1 INTO :RECORDFOUND FROM USERSPERTRAININGPROGRAM
         WHERE email = :E-MAIL
         AND tpCode = :TPCODE
       END-EXEC
       
       READCSVLINE.
       UNSTRING CSVLINE
         DELIMITED BY ","
         INTO E-MAIL LNAME FNAME
       END-UNSTRING
       
      * Functies in verband met loggen
       LOGGER.
       CALL "LOGGER"
         USING LOGLINE, OPENCLOSE
       END-CALL
       
       FSLOGGER.
       CALL "FSLOGGER"
         USING LOGLINE, FILE-STATUS
       END-CALL